package Leetcode;


import tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class rightSideView {
    List<LinkedList<Integer>> result = new LinkedList<>();
    public List<Integer> rightSideView(TreeNode root) {
        levelOrder(root);
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            res.add(result.get(i).getLast());
        }
        return res;
    }

    public List<LinkedList<Integer>> levelOrder(TreeNode root) {
        if (root == null){
            return result;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int deep = 0;
        while (!queue.isEmpty()){
            int levelSize = queue.size();
            LinkedList<Integer> path = new LinkedList<>();
            result.add(path);
            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                result.get(deep).add(node.val);
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            deep++;
        }
        return result;
    }
}
